<?php

namespace app\shop\controller;

use think\Controller;

class Cart extends Controller
{
    //构造函数
    public function __construct()
    {
        parent::__construct();
        //加载模型
        $this->userModel = model('common/User/User');
        $this->bannerModel = model('common/User/Banner');
        $this->shopModel = model('common/User/Shop');
        $this->cartModel = model('common/User/Cart');
    }


    // 步进器更新数量
    public function cartNum(){
      if($this->request->isPost()){
        $pid = (int)$this->request->param('pid');
        $num = (int)$this->request->param('num');
        $uid = (int)$this->request->param('uid');
        // halt($pid);
        $data = [
          'pid'=>$pid,
          'uid'=>$uid
        ];
        $newNum = ['num'=>$num];
        $addcartdata = $this->cartModel->where($data)->update($newNum);
        $this->success('更新数量成功',null);
        exit;
      }
    }

    // 删除购物车
    public function delCart(){
      if($this->request->isPost()){
        $id = (int)$this->request->param('id');
        // halt($id);
        $delId = ['id'=>$id];
        $delRes = $this->cartModel->destroy($delId);
        $this->success('删除成功',null);
      }
    }

    // 批量删除购物车
    public function batchDelCart(){
      if($this->request->isPost()){
        $delIds = $this->request->param('delIds');
        // halt($delIds);
        // 转成数组
        // halt(json_decode($delIds));
        $idsArray = json_decode($delIds, true); // 将 JSON 字符串转为数组
        if (is_array($idsArray) && !empty($idsArray)) {
            $batchdelRes = $this->cartModel->destroy($idsArray);
            // halt($batchdelRes);
            $this->success('删除成功',null);
            // $batchdelRes 返回删除的记录数
        } else {
            // 处理转换失败的情况
            echo "ID 格式错误";
        }
        // $batchdelRes = $this->cartModel->destroy($delIds);
        // $delId = ['id'=>$id];
        // $delRes = $this->cartModel->destroy($delId);
        // $this->success('删除成功',null);
      }
    }

}

?>

